﻿using Abp.AspNetCore.Configuration;
using Abp.AspNetCore.Mvc.ExceptionHandling;
using Abp.Authorization;
using Abp.Domain.Entities;
using Abp.Runtime.Validation;
using Abp.UI;
using Abp.Web.Models;
using KelonProud.Extensions;
using Microsoft.AspNetCore.Mvc.Filters;

namespace KelonProud.Mvc
{
    public class UnhandledExceptionFilter : AbpExceptionFilter
    {
        public UnhandledExceptionFilter(IErrorInfoBuilder errorInfoBuilder, IAbpAspNetCoreConfiguration configuration) : base(errorInfoBuilder, configuration)
        {
        }

        protected override int GetStatusCode(ExceptionContext context, bool wrapOnError)
        {
            if (!(context.Exception is AbpAuthorizationException) &&
                !(context.Exception is AbpValidationException) &&
                !(context.Exception is EntityNotFoundException) &&
                !(context.Exception is UserFriendlyException))
            {
                context.Exception = new UserFriendlyException(500, "操作失败", context.Exception.GetTrueMessage());
            }

            return base.GetStatusCode(context, wrapOnError);
        }
    }
}
